layui.define(['jquery', 'MD5'], function(exports) {
	var APPID = '20180725000188795';
	var SECRET_KEY = 'HtWtOeEA617_co8dc9eZ';

	var MD5 = layui.MD5;
	var $ = layui.jquery;

	var Translate = function(str, from, to, callback) {
		var salt = (new Date).getTime();
		
		var str1 = APPID + str + salt +SECRET_KEY;
		var sign = MD5(str1);
		
		$.ajax({
			url: 'http://api.fanyi.baidu.com/api/trans/vip/translate',
			type: 'get',
			dataType: 'jsonp',
			data: {
				q: str,
				appid: APPID,
				salt: salt,
				from: from,
				to: to,
				sign: sign
			},
			success: function(data) {
				var trans_result = data.trans_result;
				var result = '';
				for(var i = 0; i < trans_result.length; i++) {
					var tran = trans_result[i];
					result += tran.dst;
				}
				callback(result);
			}
		});
	};

	exports('Translate', Translate);
});